package com.ylbh.modules.home.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.home.dto.BusinessIntegralDTO;
import com.ylbh.common.pojo.home.dto.ClassifiedSalesDTO;
import com.ylbh.common.pojo.home.dto.OrderRingDiagramDTO;
import com.ylbh.common.pojo.home.dto.SingleQuantityTrendDTO;
import com.ylbh.common.pojo.home.vo.*;
import com.ylbh.modules.business.entity.ShoppingUser;
import com.ylbh.modules.home.mapper.HomeMapper;
import com.ylbh.modules.home.service.HomeService;
import com.ylbh.modules.sys.dao.SysUserDao;
import com.ylbh.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: czx
 * @date: 2019/08/26 16:47
 */
@Service
public class HomeServiceImpl extends ServiceImpl<HomeMapper, ShoppingUser> implements HomeService {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private HomeMapper homeMapper;

    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 首页-待处理事务
     * @return
     */
//    @Override
//    public List<HomeToBeProcessedVO> queryToBeProcessed() {
//        return homeMapper.selectToBeProcessed();
//    }
    @Override
    public List<Integer> queryToBeProcessed() {
        return homeMapper.selectToBeProcessed();
    }

    /**
     * 首页-数据概览
     * @return
     */
    @Override
    public HomeDataOverviewVO queryDataOverview() {
        // 获取当前时间的近七天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - 7);
        Date monday = c.getTime();
        String nearlySevenDays = sdf.format(monday);

        // 获取当前时间的前一天
        Calendar c1 = Calendar.getInstance();
        c1.add(Calendar.DATE, -1);
        Date start = c1.getTime();
        String yesterday = sdf.format(start);
        // 查询销售额
        HomeDataOverviewVO vo = homeMapper.selectSalesVolume(nearlySevenDays + " 00:00:00", yesterday + " 00:00:00", sdf.format(new Date()) + " 00:00:00", sdf.format(new Date()) + " 23:59:59");
        return vo;
    }

    /**
     * 首页-商城商品信息
     * @return
     */
    @Override
    public List<HomeMallInformationVO> queryMallInformation() {
        List<HomeMallInformationVO> list = homeMapper.selectMallInformation(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        for (HomeMallInformationVO vo : list) {
            // 计算环比
            double v = new BigDecimal((float) vo.getAddGoods() / vo.getAllGoods()).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double v1 = new BigDecimal((float) vo.getUpperShelf() / vo.getAllGoods()).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double v2 = new BigDecimal((float) vo.getStockShortage() / vo.getAllGoods()).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            vo.setAddGoodsChainRatio(new BigDecimal(v).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
            vo.setUpperShelfChainRatio(new BigDecimal(v1).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
            vo.setStockShortageChainRatio(new BigDecimal(v2).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
            vo.setAllGoodsChainRatio("100");
        }
        return list;
    }

    /**
     * 首页-订单环形图
     * @param orderRingDiagramDTO
     * @return
     */
    @Override
    public List<?> queryOrderRingDiagram(OrderRingDiagramDTO orderRingDiagramDTO) {
        List<HomeSourceOfOrderVO> sourceOfOrderVO = null;
        String startTime = "";
        String endTime = "";
        if ("1".equals(orderRingDiagramDTO.getTimeType())) {
            String[] time = getBeforeSevenDay(7);
            startTime = time[0];
            endTime = time[6];
        } else if ("2".equals(orderRingDiagramDTO.getTimeType())) {
            String[] time = getBeforeSevenDay(30);
            startTime = time[0];
            endTime = time[29];
        } else if (!StringUtils.isEmpty(orderRingDiagramDTO.getTimeType()) && !"1".equals(orderRingDiagramDTO.getTimeType()) && !"2".equals(orderRingDiagramDTO.getTimeType())) {
            String[] time =orderRingDiagramDTO.getTimeType().split("~");
            startTime = time[0];
            endTime = time[1];
        }
        // 根据传递的类型在查询对应的环比数据
        if (orderRingDiagramDTO.getType() == 1) {
            // 1表示商城订单;2表示联盟店订单
            if (orderRingDiagramDTO.getOrderType() == 1) {
                sourceOfOrderVO = homeMapper.selectShoppingMallSourceOfOrder(startTime, endTime, orderRingDiagramDTO.getATurnoverType());
            } else if (orderRingDiagramDTO.getOrderType() == 2) {
                sourceOfOrderVO = homeMapper.selectBusinessSourceOfOrder(startTime, endTime, orderRingDiagramDTO.getATurnoverType());
            }
            // 根据交易额类型计算环比开始
            if (orderRingDiagramDTO.getATurnoverType() == 1) {
                // 表示是单量查询，计算单量的占比
                sourceOfOrderVO = calculatingSingleQuantities(sourceOfOrderVO);
            } else if (orderRingDiagramDTO.getATurnoverType() == 2) {
                // 表示是交易额查询，计算交易额占比
                sourceOfOrderVO = calculateTransactionVolume(sourceOfOrderVO);
            }
            // 根据交易额类型计算环比结束
        } else if (orderRingDiagramDTO.getType() == 2) {
            // 然后根据类型来查询 1表示商城订单;2表示联盟店订单
            if (orderRingDiagramDTO.getOrderType() == 1) {
                sourceOfOrderVO = homeMapper.selectShoppingMallOrderType(startTime, endTime, orderRingDiagramDTO.getATurnoverType());
            } else if (orderRingDiagramDTO.getOrderType() == 2) {
                sourceOfOrderVO = homeMapper.selectBusinessOrderType(startTime, endTime, orderRingDiagramDTO.getATurnoverType());
            }
            // 根据交易额类型计算环比开始
            if (orderRingDiagramDTO.getATurnoverType() == 1) {
                // 表示是单量查询，计算单量的占比
                sourceOfOrderVO = calculatingSingleQuantities(sourceOfOrderVO);
            } else if (orderRingDiagramDTO.getATurnoverType() == 2) {
                // 表示是交易额查询，计算交易额占比
                sourceOfOrderVO = calculateTransactionVolume(sourceOfOrderVO);
            }
            // 根据交易额类型计算环比结束
        } else if (orderRingDiagramDTO.getType() == 3) {
            // 然后根据类型来查询 1表示商城订单;2表示联盟店订单
            if (orderRingDiagramDTO.getOrderType() == 1) {
                sourceOfOrderVO = homeMapper.selectShoppingMallUserType(startTime, endTime, orderRingDiagramDTO.getATurnoverType());
            } else if (orderRingDiagramDTO.getOrderType() == 2) {
                sourceOfOrderVO = homeMapper.selectBusinessUserType(startTime, endTime, orderRingDiagramDTO.getATurnoverType());
            }
            // 根据交易额类型计算环比开始
            if (orderRingDiagramDTO.getATurnoverType() == 1) {
                // 表示是单量查询，计算单量的占比
                sourceOfOrderVO = calculatingSingleQuantities(sourceOfOrderVO);
            } else if (orderRingDiagramDTO.getATurnoverType() == 2) {
                // 表示是交易额查询，计算交易额占比
                sourceOfOrderVO = calculateTransactionVolume(sourceOfOrderVO);
            }
            // 根据交易额类型计算环比结束
        }
        return sourceOfOrderVO;
    }

    /**
     * 计算单量占比
     * @param dto
     * @return
     */
    public List<HomeSourceOfOrderVO> calculatingSingleQuantities(List<HomeSourceOfOrderVO> dto) {
        int orderTotal = 0;
        for (HomeSourceOfOrderVO orderVO : dto) {
            orderTotal += orderVO.getOrderCount();
        }
        // 苹果的单量占比
        dto.get(0).setProportion((dto.get(0).getOrderCount() * 100) / orderTotal);
        // 安卓的单量占比
        dto.get(1).setProportion((dto.get(1).getOrderCount() * 100) / orderTotal);
        // 小程序的单量占比
        dto.get(2).setProportion((dto.get(2).getOrderCount() * 100) / orderTotal);
        // 微商城的单量占比
        dto.get(3).setProportion((dto.get(3).getOrderCount() * 100) / orderTotal);
        if (dto.size() == 5) {
            dto.get(4).setProportion((dto.get(4).getOrderCount() * 100) / orderTotal);
        }
        return dto;
    }

    /**
     * 计算交易额占比
     * @param dto
     * @return
     */
    public List<HomeSourceOfOrderVO> calculateTransactionVolume(List<HomeSourceOfOrderVO> dto) {
        BigDecimal orderMoney = null;
        for (HomeSourceOfOrderVO orderVO : dto) {
            if (null != orderMoney)
                if (null != orderVO.getATurnover())
                    orderMoney = orderMoney.add(orderVO.getATurnover());
            else
                orderMoney = orderVO.getATurnover();
        }
        // 苹果的交易额占比
        dto.get(0).setProportion((dto.get(0).getATurnover().multiply(new BigDecimal(100))).divide(orderMoney, 0, BigDecimal.ROUND_HALF_UP).intValue());
        // 安卓的交易额占比
        dto.get(1).setProportion((dto.get(1).getATurnover().multiply(new BigDecimal(100))).divide(orderMoney, 0, BigDecimal.ROUND_HALF_UP).intValue());
        // 小程序的交易额占比
        dto.get(2).setProportion((dto.get(2).getATurnover().multiply(new BigDecimal(100))).divide(orderMoney, 0, BigDecimal.ROUND_HALF_UP).intValue());
        // 微商城的交易额占比
        dto.get(3).setProportion((dto.get(3).getATurnover().multiply(new BigDecimal(100))).divide(orderMoney, 0, BigDecimal.ROUND_HALF_UP).intValue());
        if (dto.size() == 5) {
            dto.get(4).setProportion((dto.get(4).getATurnover().multiply(new BigDecimal(100))).divide(orderMoney, 0, BigDecimal.ROUND_HALF_UP).intValue());
        }
        return dto;
    }

    /**
     * 首页-单量走势
     * @Param singleQuantityTrendDTO
     * @return
     */
    @Override
    public HomeTrendVO querySingleQuantityTrend(SingleQuantityTrendDTO singleQuantityTrendDTO) {

        // 坐标下的时间
        ArrayList<String> indexList = new ArrayList<>();

        List<String> timeList = new ArrayList<>();
        List<String> chainRatioList = new ArrayList<>();
        List<String> yearOnYearList = new ArrayList<>();

        // 根据传过来的时间天数在查询数据开始
        if (singleQuantityTrendDTO.getTimeType() == 1) {
            // 表示查询前七天的
            String[] day = getBeforeSevenDay(7);
            for (int i = 0; i < day.length; i++) {
                timeList.add(day[i]);
                String[] split = day[i].split("-");
                indexList.add(Integer.parseInt(split[1]) + "月" + Integer.parseInt(split[2]) + "日");
                // 查询同比开始
                if (singleQuantityTrendDTO.getYearOnYear()) {
                    String yearOnYear[] = day[i].split("-");
                    yearOnYearList.add(Integer.parseInt(yearOnYear[0]) - 1 + "-" + yearOnYear[1] + "-" + yearOnYear[2]);
                }
                // 查询同比结束
            }
            // 查询环比开始
            if (singleQuantityTrendDTO.getChainRatio()) {
                String[] chainRatio = getBeforeSevenDay(14);
                for (int i = 0; i < chainRatio.length; i++) {
                    if (i < 7) {
                        chainRatioList.add(chainRatio[i]);
                    }
                }
            }
            // 查询环比结束
        } else if (singleQuantityTrendDTO.getTimeType() == 2) {
            // 表示查询前一个月的
            String[] day1 = getBeforeSevenDay(30);
            for (int i = 0; i < day1.length; i++) {
                timeList.add(day1[i]);
                if (i == 0 || i == 4 || i == 9 || i == 14 || i == 19 || i == 24 || i == 29) {
                    String[] split = day1[i].split("-");
                    indexList.add(Integer.parseInt(split[1]) + "月" + Integer.parseInt(split[2]) + "日");
                }
                // 查询同比开始
                if (singleQuantityTrendDTO.getYearOnYear()) {
                    String yearOnYear[] = day1[i].split("-");
                    yearOnYearList.add(Integer.parseInt(yearOnYear[0]) - 1 + "-" + yearOnYear[1] + "-" + yearOnYear[2]);
                }
                // 查询同比结束
            }
            // 查询环比开始
            if (singleQuantityTrendDTO.getChainRatio()) {
                String[] chainRatio = getBeforeSevenDay(60);
                for (int i = 0; i < chainRatio.length; i++) {
                    if (i < 30) {
                        chainRatioList.add(chainRatio[i]);
                    }
                }
            }
            // 查询环比结束
        } else if (singleQuantityTrendDTO.getTimeType() == 3) {
            // 表示查询前进90天的
            String[] day2 = getBeforeSevenDay(90);
            for (int i = 0; i < day2.length; i++) {
                timeList.add(day2[i]);
                if (i == 0 || i == 14 || i == 29 || i == 44 || i == 59 || i == 74 || i == 89) {
                    String[] split = day2[i].split("-");
                    indexList.add(Integer.parseInt(split[1]) + "月" + Integer.parseInt(split[2]) + "日");
                }
                // 查询同比开始
                if (singleQuantityTrendDTO.getYearOnYear()) {
                    String yearOnYear[] = day2[i].split("-");
                    yearOnYearList.add(Integer.parseInt(yearOnYear[0]) - 1 + "-" + yearOnYear[1] + "-" + yearOnYear[2]);
                }
                // 查询同比结束
            }
            // 查询环比开始
            if (singleQuantityTrendDTO.getChainRatio()) {
                String[] chainRatio = getBeforeSevenDay(180);
                for (int i = 0; i < chainRatio.length; i++) {
                    if (i < 90) {
                        chainRatioList.add(chainRatio[i]);
                    }
                }
            }
            // 查询环比结束
        }

        List<List<?>> lists = new ArrayList<>();
        // 根据传过来的时间天数在查询数据开始
        if (singleQuantityTrendDTO.getType() == 1) {
            // 查询的是商城的
            // 查询环比和同比的
            if (singleQuantityTrendDTO.getChainRatio() == true && singleQuantityTrendDTO.getYearOnYear() == true) {
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectShoppingMallTrendAll(timeList.get(i), chainRatioList.get(i), yearOnYearList.get(i));
                    lists.add(vo);
                }
            } else if (singleQuantityTrendDTO.getChainRatio() == true && singleQuantityTrendDTO.getYearOnYear() == false) {
                // 只查询环比
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectShoppingMallTrendChainRatio(timeList.get(i), chainRatioList.get(i));
                    lists.add(vo);
                }
            } else if (singleQuantityTrendDTO.getChainRatio() == false && singleQuantityTrendDTO.getYearOnYear() == true) {
                // 只查询同比
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectShoppingMallTrendYearOnYear(timeList.get(i), yearOnYearList.get(i));
                    lists.add(vo);
                }
            } else if (singleQuantityTrendDTO.getChainRatio() == false && singleQuantityTrendDTO.getYearOnYear() == false) {
                // 只查询默认的
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectShoppingMallTrend(timeList.get(i));
                    lists.add(vo);
                }
            }
        } else if (singleQuantityTrendDTO.getType() == 2) {
            // 查询的是联盟店的
            // 查询环比和同比的
            if (singleQuantityTrendDTO.getChainRatio() == true && singleQuantityTrendDTO.getYearOnYear() == true) {
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectBusinessTrendAll(timeList.get(i), chainRatioList.get(i), yearOnYearList.get(i));
                    lists.add(vo);
                }
            } else if (singleQuantityTrendDTO.getChainRatio() == true && singleQuantityTrendDTO.getYearOnYear() == false) {
                // 只查询环比
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectBusinessTrendChainRatio(timeList.get(i), chainRatioList.get(i));
                    lists.add(vo);
                }
            } else if (singleQuantityTrendDTO.getChainRatio() == false && singleQuantityTrendDTO.getYearOnYear() == true) {
                // 只查询同比
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectBusinessTrendYearOnYear(timeList.get(i), yearOnYearList.get(i));
                    lists.add(vo);
                }
            } else if (singleQuantityTrendDTO.getChainRatio() == false && singleQuantityTrendDTO.getYearOnYear() == false) {
                // 只查询默认的
                for (int i = 0; i < timeList.size(); i++) {
                    List<HomeSingleQuantityTrendVO> vo = homeMapper.selectBusinessTrend(timeList.get(i));
                    lists.add(vo);
                }
            }
        }

        HomeTrendVO homeTrendVO = new HomeTrendVO();
        homeTrendVO.setTime(indexList);
        homeTrendVO.setList(lists);
        return homeTrendVO;
    }

    /**
     * 获取时间
     * @param day
     * @return
     */
    public static String[] getBeforeSevenDay(int day){
        String [] arr = new String[day];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = null;
        for (int i=0;i< day;i++){
            c=Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, - i-1);
            arr[day-1-i] =sdf.format(c.getTime());
        }
        return arr;
    }

    /**
     * 消费排行
     * @param timeRange
     * @param type
     * @param sortType
     * @return
     */
    @Override
    public List<HomeConsumptionRankingVO> queryConsumptionRanking(String timeRange, Integer type, Boolean sortType) {
        List<HomeConsumptionRankingVO> list = null;
        if (!StringUtils.isEmpty(timeRange) && "1".equals(timeRange)) {
            String[] time = getBeforeSevenDay(7);
            list = getConsumptionRankingByType(time[0], time[6], type, sortType);
        } else if (!StringUtils.isEmpty(timeRange) && "2".equals(timeRange)) {
            String[] time = getBeforeSevenDay(30);
            list = getConsumptionRankingByType(time[0], time[29], type, sortType);
        } else if (!StringUtils.isEmpty(timeRange) && !"1".equals(timeRange) && !"2".equals(timeRange)) {
            // 表示查看的是自定义时间
            // 切割时间
            String[] times = timeRange.split("~");
            list = getConsumptionRankingByType(times[0], times[1], type, sortType);
        }
        return list;
    }

    public List<HomeConsumptionRankingVO> getConsumptionRankingByType(String startTime, String endTime, Integer type, Boolean sortType) {
        List<HomeConsumptionRankingVO> list = new ArrayList<>();
        if (type == 1) {
            // 商城热销商品排行
            list = homeMapper.selectShoppingMallSellWell(startTime, endTime, type, sortType);
        } else if (type == 2) {
            // 商城商品滞销排行
            list = homeMapper.selectShoppingMallUnsalable(startTime, endTime, type, sortType);
        } else if (type == 3) {
            // 商城商品成交转化排行
            DecimalFormat df = new DecimalFormat("0.0");
            list = homeMapper.selectConversion(startTime, endTime, type, sortType);
            for (HomeConsumptionRankingVO vo:list) {
                // 查询出对应商品的购买数量及加入购物车的数量
                List<Integer> payNum = homeMapper.selectGoodsConversion(vo.getConversionRate());
                vo.setConversionRate(df.format((float)payNum.get(0)/payNum.get(1)));
            }
        } else if (type == 4) {
            // 联盟店销量排行
            list = homeMapper.selectBusinessSalesVolume(startTime, endTime, type, sortType);
        } else if (type == 5) {
            // 热搜排行
            list = homeMapper.selectSearchVolume(startTime, endTime);
        } else if (type == 6) {
            // 会员商城消费排行
            list = homeMapper.selectShoppingMallMembershipConsumption(startTime, endTime, sortType);
        } else if (type == 7) {
            // 会员联盟店消费排行
            list = homeMapper.selectBusinessMembershipConsumption(startTime, endTime, sortType);
        } else if (type == 8) {
            HomeConsumptionRankingVO vo = new HomeConsumptionRankingVO();
            HomeConsumptionRankingVO vo1 = new HomeConsumptionRankingVO();
            HomeConsumptionRankingVO vo2 = new HomeConsumptionRankingVO();
            // 月复购率统计
            List<Integer> payMonthNum = homeMapper.selectRepeatPurchase(monthTime(), yesterdayTime());
            float conversionRate = 0f;
            if (null == payMonthNum.get(0)) {
                vo.setVolume(0);
            } else {
                vo.setVolume(payMonthNum.get(0));
            }
            if (null == payMonthNum.get(1)) {
                vo.setRepeatPurchaseCount(0);
            } else {
                vo.setRepeatPurchaseCount(payMonthNum.get(1));
            }
            vo.setVolume(vo.getVolume() + vo.getRepeatPurchaseCount());
            DecimalFormat df = new DecimalFormat("0.0");
            conversionRate = (float)vo.getRepeatPurchaseCount().intValue()/vo.getVolume() * 100;
            vo.setRepeatPurchaseRate(df.format(conversionRate));
            vo.setGoodsName("月度");
            list.add(vo);
            // 季复购率统计
            List<Integer> payQuarterNum = homeMapper.selectRepeatPurchase(quarterTime(), yesterdayTime());
            vo1.setVolume(payQuarterNum.get(0));
            vo1.setGoodsName("季度");
            float conversionRate2 = 0f;
            if (null == payQuarterNum.get(0)) {
                vo1.setVolume(0);
            } else {
                vo1.setVolume(payQuarterNum.get(0));
            }
            if (null == payQuarterNum.get(1)) {
                vo1.setRepeatPurchaseCount(0);
            } else {
                vo1.setRepeatPurchaseCount(payQuarterNum.get(1));
            }
            vo1.setVolume(vo1.getVolume() + vo1.getRepeatPurchaseCount());
            conversionRate2 = (float)vo1.getRepeatPurchaseCount().intValue()/vo1.getVolume() * 100;
            vo1.setRepeatPurchaseRate(df.format(conversionRate2));
            list.add(vo1);
            // 年复购率统计
            List<Integer> payYearNum = homeMapper.selectRepeatPurchase(yearFirst(), yesterdayTime());
            vo2.setVolume(payYearNum.get(0));
            float conversionRate3 = 0f;
            if (null == payYearNum.get(0)) {
                vo2.setVolume(0);
            } else {
                vo2.setVolume(payYearNum.get(0));
            }
            if (null == payYearNum.get(1)) {
                vo2.setRepeatPurchaseCount(0);
            } else {
                vo2.setRepeatPurchaseCount(payYearNum.get(1));
            }
            vo2.setVolume(vo2.getVolume() + vo2.getRepeatPurchaseCount());
            conversionRate3 = (float)vo2.getRepeatPurchaseCount().intValue()/vo2.getVolume() * 100;
            vo2.setGoodsName("年度");
            vo2.setRepeatPurchaseRate(df.format(conversionRate3));
            list.add(vo2);
        }
        return list;
    }


    public List<ClassifiedSalesVO> getClassifiedSales(String dayStart, String dayEnd, String thanStart, String thanEnd, Integer gcId, Integer type) {
        // 先查询近几个的
        List<ClassifiedSalesVO> list = homeMapper.selectClassifiedSalesThan(dayStart, dayEnd, thanStart, thanEnd, gcId);
        if (type == 1) {
            // 环比
            for (int i = 0; i < list.size(); i++) {
                int a = 100;
                for (int j = 0; j < list.size(); j++) {
                    if (list.get(i).getClassName().equals(list.get(j).getClassName())) {
                        a = j;
                    }
                }
                if (a != 100) {
                    list.get(i).setChainRatioSingleVolume(list.get(a).getChainRatioSingleVolume());
                    list.get(i).setChainRatioSalesVolume(list.get(a).getChainRatioSalesVolume());
                    list.remove(a);
                    a = 100;
                }
            }
        } else {
            // 同比
            for (int i = 0; i < list.size(); i++) {
                int a = 100;
                for (int j = 0; j < list.size(); j++) {
                    if (list.get(i).getClassName().equals(list.get(j).getClassName())) {
                        a = j;
                    }
                }
                if (a != 100) {
                    list.get(i).setYearOnYearSingleVolume(list.get(a).getChainRatioSingleVolume());
                    list.get(a).setChainRatioSingleVolume(0);
                    list.get(i).setYearOnYearSalesVolume(list.get(a).getChainRatioSalesVolume());
                    list.get(a).setChainRatioSalesVolume(new BigDecimal(0));
                    list.remove(a);
                    a = 100;
                }
            }
        }
        Set<ClassifiedSalesVO> treeSet = new TreeSet<ClassifiedSalesVO>(new Comparator<ClassifiedSalesVO>(){
            @Override
            public int compare(ClassifiedSalesVO o1, ClassifiedSalesVO o2) {
                int compareTo = o1.getClassName().compareTo(o2.getClassName());
                return compareTo;
            }
        });
        treeSet.addAll(list);
        list = new ArrayList<>(treeSet);
        return list;
    }

    /**
     * 商城各分类商品销量
     * @param classifiedSalesDTO
     * @return
     */
    @Override
    public List<ClassifiedSalesVO> queryClassifiedSales(ClassifiedSalesDTO classifiedSalesDTO) {
        List<ClassifiedSalesVO> list = null;
        // 当前范围的起始时间
        String dayStart = "";
        // 当前范围的结束时间
        String dayEnd = "";
        // 环比-同比的起始时间
        String thanStart = "";
        // 环比-同比的结束时间
        String thanEnd = "";
        if(classifiedSalesDTO.getChainRatio() == true && classifiedSalesDTO.getYearOnYear() == false) {
            // 只查询环比
            if(classifiedSalesDTO.getTimeType() == 1) {
                // 表示查询的是七天的
                String[] day7 = getBeforeSevenDay(14);
                thanStart = day7[0];
                thanEnd = day7[6];
                dayStart = day7[7];
                dayEnd = day7[13];
            } else if (classifiedSalesDTO.getTimeType() == 2) {
                // 表示查询的是30天的
                String[] day30 = getBeforeSevenDay(60);
                thanStart = day30[0];
                thanEnd = day30[29];
                dayStart = day30[30];
                dayEnd = day30[59];
            } else if (classifiedSalesDTO.getTimeType() == 3) {
                // 表示查询的是90天
                String[] day30 = getBeforeSevenDay(180);
                thanStart = day30[0];
                thanEnd = day30[89];
                dayStart = day30[90];
                dayEnd = day30[179];
            }
            list = getClassifiedSales(dayStart, dayEnd, thanStart, thanEnd ,classifiedSalesDTO.getGcId(), 1);
        } else if (classifiedSalesDTO.getChainRatio() == false && classifiedSalesDTO.getYearOnYear() == true) {
            // 只查询同比
            if(classifiedSalesDTO.getTimeType() == 1) {
                // 表示查询的是七天的
                String[] day7 = getBeforeSevenDay(14);
                thanStart = day7[0];
                thanEnd = day7[6];
                dayStart = day7[7];
                dayEnd = day7[13];
            } else if (classifiedSalesDTO.getTimeType() == 2) {
                // 表示查询的是30天的
                String[] day30 = getBeforeSevenDay(60);
                thanStart = day30[0];
                thanEnd = day30[29];
                dayStart = day30[30];
                dayEnd = day30[59];
            } else if (classifiedSalesDTO.getTimeType() == 3) {
                // 表示查询的是90天
                String[] day30 = getBeforeSevenDay(180);
                thanStart = day30[0];
                thanEnd = day30[89];
                dayStart = day30[90];
                dayEnd = day30[179];
            }
            list = getClassifiedSales(dayStart, dayEnd, thanStart, thanEnd ,classifiedSalesDTO.getGcId(), 2);
        } else if (classifiedSalesDTO.getChainRatio() == true && classifiedSalesDTO.getYearOnYear() == true) {
            // 同时查询同比和环比数据
            // 同比的开始时间
            String yearOrYearStartTime = "";
            // 同比的结束时间
            String yearOrYearEndTime = "";
            if(classifiedSalesDTO.getTimeType() == 1) {
                // 表示查询的是七天的
                String[] day7 = getBeforeSevenDay(14);
                thanStart = day7[0];
                thanEnd = day7[6];
                dayStart = day7[7];
                dayEnd = day7[13];
                String[] split = day7[7].split("-");
                yearOrYearStartTime = Integer.parseInt(split[0]) - 1 + "-" + split[1] + "-" + split[2];
                String[] split1 = day7[13].split("-");
                yearOrYearEndTime = Integer.parseInt(split1[0]) -1 + "-" + split1[1] + "-" + split1[2];
            } else if (classifiedSalesDTO.getTimeType() == 2) {
                // 表示查询的是30天的
                String[] day30 = getBeforeSevenDay(60);
                thanStart = day30[0];
                thanEnd = day30[29];
                dayStart = day30[30];
                dayEnd = day30[59];
                String[] split = day30[30].split("-");
                yearOrYearStartTime = Integer.parseInt(split[0]) - 1 + "-" + split[1] + "-" + split[2];
                String[] split1 = day30[59].split("-");
                yearOrYearEndTime = Integer.parseInt(split1[0]) - 1 + "-" + split1[1] + "-" + split1[2];
            } else if (classifiedSalesDTO.getTimeType() == 3) {
                // 表示查询的是90天
                String[] day30 = getBeforeSevenDay(180);
                thanStart = day30[0];
                thanEnd = day30[89];
                dayStart = day30[90];
                dayEnd = day30[179];
                String[] split = day30[90].split("-");
                yearOrYearStartTime = Integer.parseInt(split[0]) -1 + "-" + split[1] + "-" + split[2];
                String[] split1 = day30[179].split("-");
                yearOrYearEndTime = Integer.parseInt(split1[0]) -1 + "-" + split1[1] + "-" + split1[2];
            }
            list = homeMapper.selectClassifiedSalesAll(dayStart, dayEnd, thanStart, thanEnd, yearOrYearStartTime, yearOrYearEndTime, classifiedSalesDTO.getGcId());
            for (int i = 0; i < list.size(); i++) {
                int a = 100;
                for (int j = 0; j < list.size(); j++) {
                    if (list.get(i).getClassName().equals(list.get(j).getClassName())) {
                        a = j;
                    }
                }
                if (a != 100) {
                    list.get(i).setChainRatioSingleVolume(list.get(a).getChainRatioSingleVolume());
                    list.get(i).setChainRatioSalesVolume(list.get(a).getChainRatioSalesVolume());
                    list.get(i).setYearOnYearSingleVolume(list.get(a).getYearOnYearSingleVolume());
                    list.get(i).setYearOnYearSalesVolume(list.get(a).getYearOnYearSalesVolume());
                    list.remove(a);
                    a = 100;
                }
            }
            Set<ClassifiedSalesVO> treeSet = new TreeSet<ClassifiedSalesVO>(new Comparator<ClassifiedSalesVO>(){
                @Override
                public int compare(ClassifiedSalesVO o1, ClassifiedSalesVO o2) {
                    int compareTo = o1.getClassName().compareTo(o2.getClassName());
                    return compareTo;
                }
            });
            treeSet.addAll(list);
            list = new ArrayList<>(treeSet);
        } else {
            // 只查询单量
            if(classifiedSalesDTO.getTimeType() == 1) {
                // 表示查询的是七天的
                String[] day7 = getBeforeSevenDay(7);
                dayStart = day7[0];
                dayEnd = day7[6];
            } else if (classifiedSalesDTO.getTimeType() == 2) {
                // 表示查询的是30天的
                String[] day30 = getBeforeSevenDay(30);
                dayStart = day30[0];
                dayEnd = day30[29];
            } else if (classifiedSalesDTO.getTimeType() == 3) {
                // 表示查询的是90天
                String[] day30 = getBeforeSevenDay(90);
                dayStart = day30[0];
                dayEnd = day30[89];
            }
            list = homeMapper.selectSingleVolume(dayStart, dayEnd, classifiedSalesDTO.getGcId());
        }
        return list;
    }

    /**
     * 联盟店积分走势
     * @param businessIntegralDTO
     * @return
     */
    @Override
    public HomeTrendVO queryBusinessIntegral(BusinessIntegralDTO businessIntegralDTO) {
        // 坐标下的时间
        List<String> indexList = new ArrayList<>();
        List<String> timeList = new ArrayList<>();
        List<String> thanTimeList = new ArrayList<>();
        List<BusinessIntegralVO> dataList = new ArrayList<>();
        List list = null;
        String startTime = "";
        String endTime = "";
        if (businessIntegralDTO.getTimeType() == 1) {
            String[] day = getBeforeSevenDay(14);
            for (int i = 0; i < day.length; i++) {
                if (i < 7) {
                    thanTimeList.add(day[i]);
                } else {
                    timeList.add(day[i]);
                }
                if (i > 6) {
                    String[] split = day[i].split("-");
                    indexList.add(Integer.parseInt(split[1]) + "月" + Integer.parseInt(split[2]) + "日");
                }
            }
        } else if (businessIntegralDTO.getTimeType() == 2){
            String[] day = getBeforeSevenDay(60);
            for (int i = 0; i < day.length; i++) {
                if (i < 30) {
                    thanTimeList.add(day[i]);
                } else {
                    timeList.add(day[i]);
                }
                if (i == 30 || i == 34 || i == 39 || i == 44 || i == 49 || i == 54 || i == 59) {
                    String[] split = day[i].split("-");
                    indexList.add(Integer.parseInt(split[1]) + "月" + Integer.parseInt(split[2]) + "日");
                }
            }
        } else {
            String[] day = getBeforeSevenDay(180);
            for (int i = 0; i < day.length; i++) {
                if (i < 90) {
                    thanTimeList.add(day[i]);
                } else {
                    timeList.add(day[i]);
                }
                if (i == 90 || i == 104 || i == 119 || i == 134 || i == 149 || i == 164 || i == 179) {
                    String[] split = day[i].split("-");
                    indexList.add(Integer.parseInt(split[1]) + "月" + Integer.parseInt(split[2]) + "日");
                }
            }
        }
        List<List<?>> lists = new ArrayList<>();
        for (int i = 0; i < timeList.size(); i++) {
            dataList = homeMapper.queryBusinessIntegral(timeList.get(i), thanTimeList.get(i), businessIntegralDTO.getIncome(), businessIntegralDTO.getExpenditure());
            lists.add(dataList);
        }
        HomeTrendVO vo = new HomeTrendVO();
        vo.setTime(indexList);
        vo.setList(lists);
        return vo;
    }


    /**
     * 获取季度的起始时间
     * @return
     */
    public String quarterTime() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);
        return sdf.format(startCalendar.getTime());

    }

    private static void setMinTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private static void setMaxTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }

    /**
     * 月份的第一天
     * @return
     */
    public static String monthTime() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(new Date());
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMaxTime(startCalendar);
        return sdf.format(startCalendar.getTime());
    }

    /**
     * 昨天的时间
     * @return
     */
    public static String yesterdayTime() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.add(Calendar.DAY_OF_MONTH, -1);
        setMinTime(startCalendar);
        return sdf.format(startCalendar.getTime());
    }

    /**
     * 当前年份的第一天
     * @return
     */
    public static String yearFirst() {
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currentYear);
        Date currYearFirst = calendar.getTime();
        return sdf.format(currYearFirst);
    }

    /**
     * 查询出所有非公司系统用户的信息
     * @return
     */
    @Override
    public List<Integer> queryAllUserId() {
        return sysUserDao.selectAllUserId();
    }
}
